package com.online_exam.controller.student.ExamPaper;

import com.github.pagehelper.PageInfo;
import com.online_exam.base.RestResponse;
import com.online_exam.base.UserType;
import com.online_exam.domain.ExamPaper;
import com.online_exam.domain.User;
import com.online_exam.service.ExamService;
import com.online_exam.service.UserService;
import com.online_exam.viewmodel.admin.ExamPaper.ExamPaperEditRequestVM;
import com.online_exam.viewmodel.admin.ExamPaper.ExamPaperPageRequestVM;
import com.online_exam.viewmodel.admin.ExamPaper.ExamResponseVM;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.stream.Collectors;


@RestController("studentExamController")
@RequestMapping("/api/student/exam/paper")
public class ExamController {

    @Autowired
    ExamService examService;
    @Autowired
    UserService userService;
    @Autowired
    ModelMapper modelMapper;



    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo> page(@RequestBody ExamPaperPageRequestVM vm){
        PageInfo<ExamPaper> pageInfo = examService.page(vm);
        //由于泛型不同无法直接stream来setList<T>，只能重新创建新的
        PageInfo<ExamResponseVM> pageInfoVM = new PageInfo<>();
        //映射pageInfo<实体>到<VM> -只有List<T>带泛型，其他页信息属性都可以映射
        modelMapper.map(pageInfo, pageInfoVM);
        pageInfoVM.setList(pageInfo.getList().stream().map(p->modelMapper.map(p,ExamResponseVM.class)).collect(Collectors.toList()));
        System.out.println(pageInfo);
        System.out.println(pageInfoVM);
        return RestResponse.ok(pageInfoVM);
    }

    @RequestMapping(value = "/select/{id}",method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVM> select(@PathVariable Integer id){
        ExamPaperEditRequestVM examPaperEditRequestVM = examService.examPaperToVM(id);
        return RestResponse.ok(examPaperEditRequestVM);
    }
}
